<template>
  <div>
    <el-card class="box-card">
      <el-form ref="loginForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item class="login-title">
          <i style="font-size: 100px; color: #E9232c;" class="iconfont icon-apphoutaiguanli"></i>
          <h2>电商后台管理系统</h2>
        </el-form-item>

        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" type="text" placeholder="请输入用户名" prefix-icon="iconfont icon-icon" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" :type="pwdStyle" placeholder="请输入密码" prefix-icon="iconfont icon-mima">
            <template #suffix>
              <i class="iconfont" :class="pwdShowClass" style="cursor:pointer;" @click="changePwdShow"></i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="login-btn" type="danger" size="small" @click="submitForm('ruleForm')">登录</el-button>
          <el-button class="login-btn" type="warning" size="small" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <img class="login-bg" src="./../../assets/images/login_center_bg.png" alt="">
  </div>
</template>

<script>
import { ref, reactive, toRefs } from 'vue'
import { checkLogin } from '../../api/adminApi.js'
import config from '../../config/index.js'
// md5 的库 blueimp-md5
import md5 from 'blueimp-md5'
import { ElMessage } from "element-plus";
import { setLocalData } from '../../util';
import { useRouter } from 'vue-router'
export default {
  name: "Login",
  setup() {
    const router = useRouter()
    const loginForm = ref(null)
    const state = reactive({
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入登录的用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录的密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' },
        ],
      },
      pwdShowClass: 'icon-icon-test1',
      pwdStyle: 'password'
    })

    const submitForm = () => {
      loginForm.value.validate(async (valid) => {
        if (!valid) return
        // console.log(state.ruleForm, md5(state.ruleForm.password, config.KEY))
        // 调用登录接口
        const res = await checkLogin(state.ruleForm.username, md5(state.ruleForm.password, config.KEY))
        // console.log(res)
        // 判断
        if (res.status !== 1) {
          // 失败了
          return ElMessage.error(res.msg)
        }
        // 登录成功
        ElMessage.success(res.msg)
        // 进行本地存储
        setLocalData(config.S_TOKEN, res.data.token)
        setLocalData(config.S_USER, res.data)
        // 跳转到主面板
        // router.push( ... ) =>跳转到某个子页面，并在history中添加一条记录
        // router.repalce( ... ) =>替换当前的页面，其实也可以理解为跳转，和push的区别就是不会在history中添加记录
        router.replace('/')
      })
    }

    const resetForm = () => {
      loginForm.value.resetFields()
    }

    // 点击显示明文密码
    const changePwdShow = () => {
      state.pwdShowClass = state.pwdShowClass === 'icon-icon-test1' ? 'icon-icon-test' : 'icon-icon-test1'
      state.pwdStyle = state.pwdStyle === 'password' ? 'text' : 'password'
    }

    return {
      loginForm,
      // 把state转成响应式对象
      ...toRefs(state),
      submitForm,
      resetForm,
      changePwdShow
    }
  }
}
</script>

<style lang="less" scoped>
.box-card {
  width: 360px;
  position: absolute;
  left: 0;
  right: 0;
  margin: 150px auto;
  border-top: 10px solid #e9232c;
  text-align: center;
  :deep(.el-form-item__content) {
    margin: 0 !important;
  }
  .login-title {
    text-align: center;
    margin: 30px 0 10px;
    h2 {
      font-size: 24px;
      margin: 5px;
    }
  }
  .login-btn {
    width: 45%;
  }
}
.login-bg {
  background: #e9232c;
  max-width: 100%;
  margin-top: 200px;
  height: 300px;
}
</style>
